/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample.slice;

import pl.droidsonroids.gif.gifsample.LogUtil;
import pl.droidsonroids.gif.gifsample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.global.resource.RawFileDescriptor;
import pl.droidsonroids.gif.AnimationListener;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import java.io.IOException;

/**
 * AnimationSlice
 */
public class AnimationSlice extends AbilitySlice implements AnimationListener {
    private static final String TAG = AbilitySlice.class.getName();
    private static final int FOUR = 4;
    private GifDrawable drawable;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_animation);
        try {
            initView();
        } catch (IOException e) {
            LogUtil.error(TAG, "IOException");
        }
    }

    private void initView() throws IOException {
        GifImageView gif = (GifImageView) findComponentById(ResourceTable.Id_gif);

        RawFileDescriptor rawFileDescriptor = getResourceManager()
            .getRawFileEntry("entry/resources/rawfile/led7.gif").openRawFileDescriptor();

        drawable = new GifDrawable(rawFileDescriptor);
        drawable.addAnimationListener(this);
        gif.setImageElement(drawable);
        reset();

        Button start = (Button) findComponentById(ResourceTable.Id_start);
        Button stop = (Button) findComponentById(ResourceTable.Id_stop);
        Button reset = (Button) findComponentById(ResourceTable.Id_reset);

        start.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                drawable.getAnimator().start();
            }
        });

        stop.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                drawable.getAnimator().stop();
            }
        });

        reset.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                reset();
            }
        });
    }

    private void reset() {
        drawable.getAnimator().stop();
        drawable.setLoopCount(FOUR);
        drawable.seekToFrameAndGet(1);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onAnimationCompleted(int loopNumber) {
        if (loopNumber == drawable.getLoopCount() - 1) {
            reset();
        }
    }
}
